﻿package com.common.queueloader
{
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;		
	/**
	 *队列加载器 
	 * @author lake
	 * 
	 */	
	public class QueueLoader extends EventDispatcher
	{
		
		/***************************************
		******	 LOADABLE ITEM LIST   **********
		***************************************/
		/**
		 * swf
		 */		
		public static const FILE_SWF : int = 1;
		/**
		 * xml
		 */		
		public static const FILE_XML : int = 2;
		/**
		 * zip
		 */		
		public static const FILE_ZIP : int = 3;
		/**
		 * tga
		 */		
		public static const FILE_TGA : int = 4;
		/**
		 * jpg
		 */		
		public static const FILE_JPG : int = 5;
		/**
		 * png
		 */		
		public static const FILE_PNG : int = 6;
		/**
		 * csv
		 */		
		public static const FILE_CSV : int = 7;
		/**
		 * 
		 */		
		public static const FILE_TXT : int = 8;
		/**
		 * lzm
		 */		
		public static const FILE_LZM : int = 9;
		/**
		 * swc
		 */		
		public static const FILE_SWC : int = 10;
		/**
		 * czip
		 */		
		public static const FILE_CZIP : int = 11;
		/**
		 *资源地址  
		 */		
		public static var swfPath		:String = "";
		/**
		 * 加载列表
		 */		
		protected var loadingQueue : Array;
		/**
		 * 当前加载item
		 */		
		protected var currItem:ILoadable;		/**
		 * 是否加载中
		 */		
		protected var _isLoading:Boolean;
		/**
		 * 加载数目
		 */		
		protected var _queueItemNum:int;
		
		public function QueueLoader() 
		{
			_isLoading = false;
			loadingQueue = new Array;
			ItemList.initItems();
		}
		/**
		 * 加入任务 
		 * @param queueVo
		 * @param url
		 * @return 
		 */		
		public function addQueue(queueVo:QueueVo,url:String):Boolean
		{
			var arr:Array = url.split(".");
			var reasult:Boolean = false;
			for(var i:String in ItemList.itemType)
			{
				var exp:String = ItemList.itemType[i].regEx;
				if(exp==arr[arr.length-1])
				{
					reasult = true;
					//判断是否有一样的url
					for(var j:int=0;j<loadingQueue.length;j++)
					{
						if(url==(loadingQueue[j] as ILoadable).path.url)
						{
							return false;
						}
					}
					loadingQueue.push(new ItemList.itemType[i].classRef( queueVo , new URLRequest(swfPath+url),i));
				}
			}
			return reasult;
		}
		/**
		 *是否已经在队列中 
		 * @param url
		 * @return 
		 */		
		public function isQueue( url:String ):Boolean
		{
			for( var i:int = 0 ; i < loadingQueue.length ; i++ )
			{
				if ( (loadingQueue[i] as AbstractItem).path.url == url )
				return true;
			}
			return false;
		}
		/**
		 * 移除任务 
		 * @param index
		 * @return 
		 * 
		 */		
		public function removeQueueAt(index:int) : Array 
		{
			return loadingQueue.splice(index,1);
		}
		/**
		 * 执行加载 
		 * 
		 */		
		public function queueLoadStart() : void 
		{
			if(_isLoading)
			{
				return;
			}
			else
			{
				_isLoading = true;
				if(loadingQueue.length>0)
				{
					_queueItemNum = loadingQueue.length;
					loadNextItem();
					dispatchEvent(new QueueLoaderEvent( QueueLoaderEvent.QUEUE_START ) );
				}
			}
		}
		/**
		 * 暂停下载 
		 * 
		 */		
		public function queueLoadPause():void
		{
			_isLoading = false;
		}
		public function get loading() : Boolean 
		{
			return _isLoading;
		}
		public function get queueItemNum() : int 
		{
			return _queueItemNum;
		}
		/**
		 *销毁 
		 */		
		public function dispose() : void 
		{
			for(var i:int=0;i<loadingQueue.length;i++)
			{
				loadingQueue.splice(i);
			}
		}
		/**
		 *加载下个item 
		 * 
		 */		
		protected function loadNextItem() : void 
		{
			if(!_isLoading)return;
			if(loadingQueue.length>0)
			{
				currItem = loadingQueue[0] as ILoadable;
				currItem.registerItem(itemCompleteHandler,itemProgressHandler ,itemHttpStatusHandler,itemErrorHandler );				
				currItem.load();
			}
			else
			{
				this.dispose();
				_isLoading = false;
				dispatchEvent( new QueueLoaderEvent(QueueLoaderEvent.QUEUE_COMPLETE));
			}
		}
		/**
		 *item 加载完毕 
		 * @param evt
		 * 
		 */		   
		public function itemCompleteHandler(evt:*):void 
		{
			var percent:int = (_queueItemNum - loadingQueue.length) + 1;
			dispatchEvent( new QueueLoaderEvent(QueueLoaderEvent.QUEUE_PROGRESS,currItem,percent));
				
			dispatchEvent( new QueueLoaderEvent( QueueLoaderEvent.ITEM_COMPLETE,currItem ) );
			this.removeQueueAt(0);
			currItem = null;
			loadNextItem();
		}
		/**
		 *item 加载进度 
		 * @param evt
		 * 
		 */		
		public function itemProgressHandler(evt:ProgressEvent):void
		{
			dispatchEvent( new QueueLoaderEvent( QueueLoaderEvent.ITEM_PROGRESS , currItem ) );
		}
		/**
		 *item http 状态事件 
		 * @param evt
		 * 
		 */		
		public function itemHttpStatusHandler(evt:*):void
		{
			trace("沙箱安全问题");
			dispatchEvent(new QueueLoaderEvent(QueueLoaderEvent.ITEM_HTTP_STATUS,currItem));
		}
		/**
		 *item 加载错误 
		 * @param evt
		 * 
		 */		
		public function itemErrorHandler(evt:*):void
		{
			trace("文件路径有误",currItem.path.url);
			dispatchEvent(new QueueLoaderEvent(QueueLoaderEvent.ITEM_ERROR,currItem));
			this.removeQueueAt(0);
			currItem = null;
			loadNextItem();
		}
	}
}